import * as THREE from 'three';
import { modelAnimate } from './modelOpera.js';
// import { HouseTest } from './house.js';
import { JKFrameAdd, JKFrameRemove } from './JKFrame.js';// JK架
import { G1FrameAdd, G1FrameRemove } from './G1Frame.js';// G1架
import { G2FrameAdd, G2FrameRemove } from './G2Frame.js';// G2架
import { GL1FrameAdd, GL1FrameRemove } from './GL1Frame.js';// GL1架
import { Z1FrameAdd, Z1FrameRemove } from './Z1Frame.js';// Z1架
import { Z2FrameAdd, Z2FrameRemove } from './Z2Frame.js';// Z2架
import { Z3FrameAdd, Z3FrameRemove } from './Z3Frame.js';// Z3架
import { Z4FrameAdd, Z4FrameRemove } from './Z4Frame.js';// Z4架
import { Z5FrameAdd, Z5FrameRemove } from './Z5Frame.js';// Z5架 
import { ZY1FrameAdd, ZY1FrameRemove } from './ZY1Frame.js';// ZY1架
import { ZY2FrameAdd, ZY2FrameRemove } from './ZY2Frame.js';// ZY2架
import { Z6FrameAdd, Z6FrameRemove } from './Z6Frame.js';// Z6架
import { Z7FrameAdd, Z7FrameRemove } from './Z7Frame.js';// Z7架
import { Z8FrameAdd, Z8FrameRemove } from './Z8Frame.js';// Z8架
import { JZJGFrameAdd, JZJGFrameRemove } from './JZJGFrame.js';// 计轴机柜
import { WJJCJGFrameAdd, WJJCJGFrameRemove } from './WJJCJGFrame.js';// 微机监测机柜
import { LSAJGFrameAdd, LSAJGFrameRemove } from './LSAJGFrame.js';// 联锁A机柜
import { LSBJGFrameAdd, LSBJGFrameRemove } from './LSBJGFrame.js';// 联锁B机柜
import { FLGFrameAdd, FLGFrameRemove } from './FLGFrame.js';// 防雷柜
import { CTCGKJGFrameAdd, CTCGKJGFrameRemove } from './CTCGKJGFrame.js';// CTC工控机柜
import { CTCCJJGFrameAdd, CTCCJJGFrameRemove } from './CTCCJJGFrame.js';// CTC采集机柜
import { DCJKGFrameAdd, DCJKGFrameRemove } from './DCJKGFrame.js';// 调车监控柜

class ModelClick {
  constructor(scene, renderer, camera, mixer, callback) {
    let state = {};
    let raycaster = new THREE.Raycaster();// 光线投射器
    let mouse = new THREE.Vector2();// 二维向量  

    document.addEventListener('click', (e) => {
      const ev = e || window.event;
      ev.preventDefault();

      switch (ev.target.id) {
        case 'JKFrameAdd':
          if (!state.JKFrameAdd) {
            state.JKFrameAdd = true;
            new JKFrameAdd(scene);
          }
          break;
        case 'JKFrameRemove':
          state.JKFrameAdd = false;
          new JKFrameRemove(scene);
          break;
        // case 'JKFrameView':
        // 	if (state.JKFrameAdd) {
        // 		controls.reset();
        // 		modelAnimate(camera, 1, [
        // 			new THREE.KeyframeTrack('camera.position', [0, 1], [state.cameraX, state.cameraY, state.cameraZ, 260, 156, 144])
        // 		]);
        // 	}
        // 	break;
        case 'G1FrameAdd':
          if (!state.G1FrameAdd) {
            state.G1FrameAdd = true;
            new G1FrameAdd(scene);
          }
          break;
        case 'G1FrameRemove':
          state.G1FrameAdd = false;
          new G1FrameRemove(scene);
          break;
        case 'G2FrameAdd':
          if (!state.G2FrameAdd) {
            state.G2FrameAdd = true;
            new G2FrameAdd(scene);
          }
          break;
        case 'G2FrameRemove':
          state.G2FrameAdd = false;
          new G2FrameRemove(scene);
          break;
        case 'GL1FrameAdd':
          if (!state.GL1FrameAdd) {
            state.GL1FrameAdd = true;
            new GL1FrameAdd(scene);
          }
          break;
        case 'GL1FrameRemove':
          state.GL1FrameAdd = false;
          new GL1FrameRemove(scene);
          break;
        case 'Z1FrameAdd':
          if (!state.Z1FrameAdd) {
            state.Z1FrameAdd = true;
            new Z1FrameAdd(scene);
          }
          break;
        case 'Z1FrameRemove':
          state.Z1FrameAdd = false;
          new Z1FrameRemove(scene);
          break;
        case 'Z2FrameAdd':
          if (!state.Z2FrameAdd) {
            state.Z2FrameAdd = true;
            new Z2FrameAdd(scene);
          }
          break;
        case 'Z2FrameRemove':
          state.Z2FrameAdd = false;
          new Z2FrameRemove(scene);
          break;
        case 'Z3FrameAdd':
          if (!state.Z3FrameAdd) {
            state.Z3FrameAdd = true;
            new Z3FrameAdd(scene);
          }
          break;
        case 'Z3FrameRemove':
          state.Z3FrameAdd = false;
          new Z3FrameRemove(scene);
          break;
        case 'Z4FrameAdd':
          if (!state.Z4FrameAdd) {
            state.Z4FrameAdd = true;
            new Z4FrameAdd(scene);
          }
          break;
        case 'Z4FrameRemove':
          state.Z4FrameAdd = false;
          new Z4FrameRemove(scene);
          break;
        case 'Z5FrameAdd':
          if (!state.Z5FrameAdd) {
            state.Z5FrameAdd = true;
            new Z5FrameAdd(scene);
          }
          break;
        case 'Z5FrameRemove':
          state.Z5FrameAdd = false;
          new Z5FrameRemove(scene);
          break;
        case 'ZY1FrameAdd':
          if (!state.ZY1FrameAdd) {
            state.ZY1FrameAdd = true;
            new ZY1FrameAdd(scene);
          }
          break;
        case 'ZY1FrameRemove':
          state.ZY1FrameAdd = false;
          new ZY1FrameRemove(scene);
          break;
        case 'ZY2FrameAdd':
          if (!state.ZY2FrameAdd) {
            state.ZY2FrameAdd = true;
            new ZY2FrameAdd(scene);
          }
          break;
        case 'ZY2FrameRemove':
          state.ZY2FrameAdd = false;
          new ZY2FrameRemove(scene);
          break;
        case 'Z6FrameAdd':
          if (!state.Z6FrameAdd) {
            state.Z6FrameAdd = true;
            new Z6FrameAdd(scene);
          }
          break;
        case 'Z6FrameRemove':
          state.Z6FrameAdd = false;
          new Z6FrameRemove(scene);
          break;
        case 'Z7FrameAdd':
          if (!state.Z7FrameAdd) {
            state.Z7FrameAdd = true;
            new Z7FrameAdd(scene);
          }
          break;
        case 'Z7FrameRemove':
          state.Z7FrameAdd = false;
          new Z7FrameRemove(scene);
          break;
        case 'Z8FrameAdd':
          if (!state.Z8FrameAdd) {
            state.Z8FrameAdd = true;
            new Z8FrameAdd(scene);
          }
          break;
        case 'Z8FrameRemove':
          state.Z8FrameAdd = false;
          new Z8FrameRemove(scene);
          break;
        case 'JZJGFrameAdd':
          if (!state.JZJGFrameAdd) {
            state.JZJGFrameAdd = true;
            new JZJGFrameAdd(scene);
          }
          break;
        case 'JZJGFrameRemove':
          state.JZJGFrameAdd = false;
          new JZJGFrameRemove(scene);
          break;
        case 'WJJCJGFrameAdd':
          if (!state.WJJCJGFrameAdd) {
            state.WJJCJGFrameAdd = true;
            new WJJCJGFrameAdd(scene);
          }
          break;
        case 'WJJCJGFrameRemove':
          state.WJJCJGFrameAdd = false;
          new WJJCJGFrameRemove(scene);
          break;
        case 'LSAJGFrameAdd':
          if (!state.LSAJGFrameAdd) {
            state.LSAJGFrameAdd = true;
            new LSAJGFrameAdd(scene);
          }
          break;
        case 'LSAJGFrameRemove':
          state.LSAJGFrameAdd = false;
          new LSAJGFrameRemove(scene);
          break;
        case 'LSBJGFrameAdd':
          if (!state.LSBJGFrameAdd) {
            state.LSBJGFrameAdd = true;
            new LSBJGFrameAdd(scene);
          }
          break;
        case 'LSBJGFrameRemove':
          state.LSBJGFrameAdd = false;
          new LSBJGFrameRemove(scene);
          break;
        case 'FLGFrameAdd':
          if (!state.FLGFrameAdd) {
            state.FLGFrameAdd = true;
            new FLGFrameAdd(scene);
          }
          break;
        case 'FLGFrameRemove':
          state.FLGFrameAdd = false;
          new FLGFrameRemove(scene);
          break;
        case 'CTCGKJGFrameAdd':
          if (!state.CTCGKJGFrameAdd) {
            state.CTCGKJGFrameAdd = true;
            new CTCGKJGFrameAdd(scene);
          }
          break;
        case 'CTCGKJGFrameRemove':
          state.CTCGKJGFrameAdd = false;
          new CTCGKJGFrameRemove(scene);
          break;
        case 'CTCCJJGFrameAdd':
          if (!state.CTCCJJGFrameAdd) {
            state.CTCCJJGFrameAdd = true;
            new CTCCJJGFrameAdd(scene);
          }
          break;
        case 'CTCCJJGFrameRemove':
          state.CTCCJJGFrameAdd = false;
          new CTCCJJGFrameRemove(scene);
          break;
        case 'DCJKGFrameAdd':
          if (!state.DCJKGFrameAdd) {
            state.DCJKGFrameAdd = true;
            new DCJKGFrameAdd(scene);
          }
          break;
        case 'DCJKGFrameRemove':
          state.DCJKGFrameAdd = false;
          new DCJKGFrameRemove(scene);
          break;
      }
      // 点击模型
      mouse.x = (event.clientX / renderer.domElement.clientWidth) * 2 - 1;
      mouse.y = -(event.clientY / renderer.domElement.clientHeight) * 2 + 1;
      raycaster.setFromCamera(mouse, camera);
      const intersects = raycaster.intersectObjects(scene.children);
      if (intersects.length > 0) {
        for (const item of intersects) {
          switch (item.object.name) {
            case '开关拨片':
              if (item.object.animateStep) {
                item.object.animateStep = false;
                new modelAnimate(mixer, item.object, 0.2, [
                  new THREE.KeyframeTrack('item.rotation[z]', [0, 0.2], [-Math.PI / 2, 0]),
                  new THREE.KeyframeTrack('item.position[x]', [0, 0.2], [-Math.PI / 8, 0]),
                  new THREE.KeyframeTrack('item.position[y]', [0, 0.2], [Math.PI / 8, 0])
                ], (newMixer) => {
                  callback(newMixer);
                });
              } else {
                item.object.animateStep = true;
                new modelAnimate(mixer, item.object, 0.2, [
                  new THREE.KeyframeTrack('item.rotation[z]', [0, 0.2], [0, -Math.PI / 2]),
                  new THREE.KeyframeTrack('item.position[x]', [0, 0.2], [0, -Math.PI / 8]),
                  new THREE.KeyframeTrack('item.position[y]', [0, 0.2], [0, Math.PI / 8])
                ], (newMixer) => {
                  callback(newMixer);
                });
              }
              return;
          }
        }
      }
    }, false);
  }
}
export { ModelClick }; 